/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Services;

import Entities.Movie;
import java.util.ArrayList;
import java.util.Date;
import javax.persistence.*;
import java.util.List;

/**
 *
 * @author Edwin
 */
public class MovieService {

    protected EntityManager em;

    public MovieService(EntityManager em) {
        this.em = em;
    }

    public Movie createMovie(String name,Integer year,
            String cover,String genre,String plotKeywords,Float rating,
            Float duration,Boolean availableForRental,Boolean availableForPurchase) {
        Movie movie = new Movie();
        movie.setName(name);
        movie.setYear(year);
        movie.setCover(cover);
        movie.setGenre(genre);
        movie.setPlotKeywords(plotKeywords);
        movie.setRating(rating);
        movie.setDuration(duration);
        movie.setAvailableForRental(availableForRental);
        movie.setAvailableForPurchase(availableForPurchase);
                
        em.persist(movie);
        
        return movie;
    }
    
    
    public void updateMovie(Long id,String name,Integer year,
            String cover,String genre,String plotKeywords,Float rating,
            Float duration,Boolean availableForRental,Boolean availableForPurchase) {
        Movie movie = findMovie(id);
        movie.setName(name);
        movie.setYear(year);
        movie.setCover(cover);
        movie.setGenre(genre);
        movie.setPlotKeywords(plotKeywords);
        movie.setRating(rating);
        movie.setDuration(duration);
        movie.setAvailableForRental(availableForRental);
        movie.setAvailableForPurchase(availableForPurchase);
        
        em.persist(movie);
    }  
        
    public void removeMovie(Long ID) {
        Movie movie = em.find(Movie.class, ID);
        System.out.println(movie.getName());
        if (movie != null) {
            
//            em.createNamedQuery("Movie.deleteByID").setParameter("id", '%' + ID + "%");
           
            em.remove(movie);
            System.out.println("Deleted the matrix!");
        }
    }

    public Movie findMovie(Long ID) {
        return em.find(Movie.class, ID);
    }
    public List<Movie> findByName(String name){      
        return em.createNamedQuery("getMovieByName").setParameter("name", '%' + name.toLowerCase() + "%").getResultList();
    }
    public List<Movie> findAll(){      
        return em.createNamedQuery("getAll").getResultList();
    }    
}
